package com.hxkcloud.patient.aerotel.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.regex.Pattern;

public class StringUtil {
    private static final Pattern FIND_COLON_PATTERN = Pattern
            .compile("(?<!\\\\):");
    private static final Pattern NUMERIC_PATTERN = Pattern
            .compile("(\\d+\\.)?\\d+");
    private static final String[] hexchars = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};

    public static boolean isNullOrEmpty(String str) {
        return str == null || "".equals(str.trim());
    }

    public static boolean isNotNullOrEmpty(String s) {
        return !isNullOrEmpty(s);
    }

    public static boolean isNumeric(String str) {
        return NUMERIC_PATTERN.matcher(str).matches();
    }

    public static String escape(String subject) {
        String encoding = "UTF-8";
        try {
            String s = new String(subject.getBytes(), encoding);
            return URLEncoder.encode(s, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String unescape(String subject) {
        String encoding = "UTF-8";
        try {
            return URLDecoder.decode(subject, encoding);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String capitalize(String s) {
        char[] chars = s.toCharArray();
        chars[0] = Character.toUpperCase(chars[0]);
        return chars.toString();
    }

    public static String asc2uni(String inString) {

        int sa = 0, sb = 0, sc = 0, sd = 0;
        String uniStr = "";
        if (inString == null || inString.length() == 0 || inString.equals("")) {
            return "";
        }
        for (int ata = 0; ata < inString.length(); ata++) {
            sb = (inString.charAt(ata)) % 256; // sbb.charAt(ata))%
            // 256; //get the low
            // byte of the unicode
            sa = (sb / 16);
            sb = sb % 16;
            sd = (inString.charAt(ata)) / 256; // sbb.charAt(ata)) /
            // 256; //get the high
            // byte of the unicode
            sd = sd % 256;
            sc = (sd / 16);
            sd = sd % 16;
            uniStr += "\\u" + hexchars[sc] + hexchars[sd] + hexchars[sa]
                    + hexchars[sb];
        }
        return (uniStr);
    }
    public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }
    public static boolean hasLength(String str) {
        return ((str != null) && (str.length() > 0));
    }

    public static boolean hasText(String str) {
        if (!(hasLength(str))) {
            return false;
        }
        int strLen = str.length();
        for (int i = 0; i < strLen; ++i) {
            if (!(Character.isWhitespace(str.charAt(i)))) {
                return true;
            }
        }
        return false;
    }

    public static boolean containsWhitespace(String str) {
        if (!(hasLength(str))) {
            return false;
        }
        int strLen = str.length();
        for (int i = 0; i < strLen; ++i) {
            if (Character.isWhitespace(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }

    public static String trimWhitespace(String str) {
        if (!(hasLength(str))) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        do {
            sb.deleteCharAt(0);

            if (sb.length() <= 0)
                break;
        } while (Character.isWhitespace(sb.charAt(0)));

        while ((sb.length() > 0)
                && (Character.isWhitespace(sb.charAt(sb.length() - 1)))) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    public static String trimAllWhitespace(String str) {
        if (!(hasLength(str))) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        int index = 0;
        while (sb.length() > index) {
            if (Character.isWhitespace(sb.charAt(index))) {
                sb.deleteCharAt(index);
            } else {
                ++index;
            }
        }
        return sb.toString();
    }

    public static String trimLeft(String str) {
        if (!(hasLength(str))) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        while ((sb.length() > 0) && (Character.isWhitespace(sb.charAt(0)))) {
            sb.deleteCharAt(0);
        }
        return sb.toString();
    }

    public static String trimRight(String str) {
        if (!(hasLength(str))) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        while ((sb.length() > 0)
                && (Character.isWhitespace(sb.charAt(sb.length() - 1)))) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    public static String trimeLeft(String str, char leadingCharacter) {
        if (!(hasLength(str))) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        while ((sb.length() > 0) && (sb.charAt(0) == leadingCharacter)) {
            sb.deleteCharAt(0);
        }
        return sb.toString();
    }

    public static String trimRight(String str, char trailingCharacter) {
        if (!(hasLength(str))) {
            return str;
        }
        StringBuilder sb = new StringBuilder(str);
        while ((sb.length() > 0)
                && (sb.charAt(sb.length() - 1) == trailingCharacter)) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    public static String format(String pattern, String... arguments) {
        String result = null;
        Object[] objects = new Object[arguments.length];
        for (int i = 0; i < arguments.length; i++) {
            objects[i] = arguments[i];
        }
        result = MessageFormat.format(pattern, objects);
        return result;
    }

    public static String format(String pattern, Object... arguments) {
        return MessageFormat.format(pattern, arguments);
    }

    public static String join(Object[] objects, String delimiter) {
        StringBuffer sb = new StringBuffer();
        if (objects != null && objects.length > 0) {
            sb.append(objects[0]);
            for (int i = 1; i < objects.length; i++) {
                sb.append(delimiter).append(objects[i]);
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        // System.out.println(isNumeric("0.123"));
        // System.out.println(isNumeric(".123"));
        // System.out.println(isNumeric("123"));
        // System.out.println(isNumeric("0.sdf"));
        System.out.println(StringUtil.escape("Sat Oct 05 15:39:32 CST 2016"));
    }
}
